# Maintainer: Evangelos Foutras <evangelos@foutrelis.com>
# Contributor: Jan "heftig" Steffens <jan.steffens@gmail.com>

pkgname=('llvm12' 'llvm12-libs')
pkgver=12.0.1
pkgrel=1
arch=('x86_64')
url="https://llvm.org/"
license=('custom:Apache 2.0 with LLVM Exception')
makedepends=('cmake' 'ninja' 'libffi' 'libedit' 'ncurses' 'libxml2'
             'python-setuptools')
options=('staticlibs')
_source_base=https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver
source=($_source_base/llvm-$pkgver.src.tar.xz{,.sig}
        llvm-link-with-Bsymbolic-functions.patch
        add-fno-semantic-interposition.patch
        x86-twist-shuffle-mask.patch
        no-strict-aliasing-DwarfCompileUnit.patch)
sha256sums=('7d9a8405f557cefc5a21bf5672af73903b64749d9bc3a50322239f56f34ffddf'
            'SKIP'
            '560ce1e206c19f4b86f4c583b743db0ad47a610418999350710aafd60ae50fcd'
            'fc8c64267a5d179e9fc24fb2bc6150edef2598c83f5b2d138d14e05ce9f4e345'
            'c51b8754f76eb3774f46d530409f6d89f5bb47d90f0d718dbfa861f716b29693'
            'd1eff24508e35aae6c26a943dbaa3ef5acb60a145b008fd1ef9ac6f6c4faa662')
validpgpkeys+=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D') # Hans Wennborg <hans@chromium.org>
validpgpkeys+=('474E22316ABF4785A88C6E8EA2C794A986419D8A') # Tom Stellard <tstellar@redhat.com>

# Utilizing LLVM_DISTRIBUTION_COMPONENTS to avoid
# CMake reference to LLVMgold; inspired by Gentoo
_get_distribution_components() {
  local target
  ninja -t targets | grep -Po 'install-\K.*(?=-stripped:)' | while read -r target; do
    case $target in
      llvm-libraries|distribution)
        continue
        ;;
      LLVMgold)
        continue
        ;;
    esac
    echo $target
  done
}

prepare() {
  cd "$srcdir/llvm-$pkgver.src"
  mkdir build

  # https://bugs.archlinux.org/task/70697
  patch -Np2 -i ../llvm-link-with-Bsymbolic-functions.patch
  # https://reviews.llvm.org/D102453
  patch -Np2 -i ../add-fno-semantic-interposition.patch

  # https://bugs.llvm.org/show_bug.cgi?id=50823
  patch -Np2 -i ../x86-twist-shuffle-mask.patch

  # https://bugs.llvm.org/show_bug.cgi?id=50611#c3
  patch -Np2 -i ../no-strict-aliasing-DwarfCompileUnit.patch
}

build() {
  cd "$srcdir/llvm-$pkgver.src/build"

  local cmake_args=(
    -G Ninja
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=/usr
    -DLLVM_HOST_TRIPLE=$CHOST
    -DLLVM_BUILD_LLVM_DYLIB=ON
    -DLLVM_LINK_LLVM_DYLIB=ON
    -DLLVM_INSTALL_UTILS=ON
    -DLLVM_ENABLE_RTTI=ON
    -DLLVM_ENABLE_FFI=ON
    -DLLVM_BUILD_TESTS=ON
    -DLLVM_BINUTILS_INCDIR=/usr/include
  )

  cmake .. "${cmake_args[@]}"
  local distribution_components=$(_get_distribution_components | paste -sd\;)
  test -n "$distribution_components"
  cmake_args+=(-DLLVM_DISTRIBUTION_COMPONENTS="$distribution_components")

  cmake .. "${cmake_args[@]}"
  ninja
}

check() {
  cd "$srcdir/llvm-$pkgver.src/build"
  ninja check
}

package_llvm12() {
  pkgdesc="Collection of modular and reusable compiler and toolchain technologies"
  depends=('llvm12-libs' 'perl')
  conflicts=('llvm')

  cd "$srcdir/llvm-$pkgver.src/build"

  DESTDIR="$pkgdir" ninja install-distribution

  # Include lit for running lit-based tests in other projects
  pushd ../utils/lit
  python3 setup.py install --root="$pkgdir" -O1
  popd

  # The runtime libraries go into llvm12-libs
  mv -f "$pkgdir"/usr/lib/lib{LLVM-*.so,{LTO,Remarks}.so.*} "$srcdir"

  # Remove files which conflict with llvm-libs
  rm "$pkgdir"/usr/lib/lib{LLVM,LTO,Remarks}.so

  install -Dm644 ../LICENSE.TXT "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_llvm12-libs() {
  pkgdesc="LLVM 11 runtime libraries"
  depends=('gcc-libs' 'zlib' 'libffi' 'libedit' 'ncurses' 'libxml2')

  install -d "$pkgdir/usr/lib"
  cp -P "$srcdir"/lib{LLVM-*.so,{LTO,Remarks}.so.*} "$pkgdir/usr/lib/"

  install -Dm644 "$srcdir/llvm-$pkgver.src/LICENSE.TXT" \
    "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

# vim:set ts=2 sw=2 et:
